package com.zh.common.plugins.shiro;

import com.zh.common.plugins.shiro.constant.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @ProjectName: healthCare-parent
 * @Package: com.zh.common.plugins.shiro
 * @ClassName: TokeService
 * @Description: 存储redis
 * @Author: lds
 * @CreateDate: 2019/5/23 0023 16:26
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/5/23 0023 16:26
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
public class TokeService {
    //
    @Value("${redis.identifyingTokenExpireTime}")
    private long identifyingTokenExpireTime;
    //redis中jwtToken过期时间
    @Value("${redis.refreshJwtTokenExpireTime}")
    private long refreshJwtTokenExpireTime;

    @Autowired
    RedisTemplate redisTemplate;

    public void addTokenToRedis(String userName,int userType, String jwtTokenStr) {
        String key = CommonConstant.JWT_TOKEN +":"+userType+":"+userName;
        redisTemplate.opsForValue().set(key, jwtTokenStr, refreshJwtTokenExpireTime, TimeUnit.MINUTES);
    }

    public String getTokenFromRedis(String userName,int userType) {
        String key = CommonConstant.JWT_TOKEN +":"+userType+":"+userName;
        return (String) redisTemplate.opsForValue().get(key);
    }

    public Map<String, Object> createRandomToken(String textStr) {
        //生成一个token
        String sToken = UUID.randomUUID().toString();
        //生成验证码对应的token  以token为key  验证码为value存在redis中
        redisTemplate.opsForValue().set(CommonConstant.NO_REPEAT_PRE + sToken, textStr, identifyingTokenExpireTime, TimeUnit.MINUTES);
        Map<String, Object> map = new HashMap<>();
        map.put("cToken", sToken);
        return map;
    }

}
